package com.dgut.courses_selecting_sys.controller;


import com.dgut.courses_selecting_sys.entity.AjaxResult;
import com.dgut.courses_selecting_sys.entity.Dept;
import com.dgut.courses_selecting_sys.service.IDeptService;
import com.dgut.courses_selecting_sys.utils.ShiroUtils;
import com.dgut.courses_selecting_sys.vo.TreeNode;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author yuec
 * @since 2020-08-06
 */
@Controller
@RequestMapping("//dept")
public class DeptController {

    @Autowired
    IDeptService deptService;

    @RequiresRoles("admin")
    @GetMapping()
    public String dept() {
        return "admin/depart";
    }

    @ResponseBody
    @GetMapping("/getAncestorsFrom")
    public AjaxResult getAncestorsFrom() {
        Dept dept = deptService.getByDeptId(ShiroUtils.getSysUser().getDeptId());
        return AjaxResult.success(deptService.getAncestorsFrom(dept));

    }

    /**
     * 学校结构树转换---细分到学院
     */
    @ResponseBody
    @PostMapping("/getDeptData")
    public List<TreeNode> getDeptData() {
        return deptService.getDeptData();
    }


    /**
     * 学校结构树转换---细分到班级
     *
     * @return
     */
    @ResponseBody
    @PostMapping("/getDeptDataDetails")
    public List<TreeNode> getDeptDataDetails() {
        return deptService.getDeptDataDetails();
    }


    /**
     * 删除结构树某节点
     *
     * @param deptId
     * @return
     */
    @RequiresRoles("admin")
    @ResponseBody
    @PostMapping("/removeDept")
    public AjaxResult removeDept(int deptId) {
        if (deptService.removeDeptById(deptId)) {
            return AjaxResult.success("删除成功");
        } else {
            return AjaxResult.error("删除失败");
        }
    }

    /**
     * 编辑结构树某节点（修改名字）
     *
     * @param deptId
     * @param deptName
     * @return
     */
    @RequiresRoles("admin")
    @ResponseBody
    @PostMapping("/alterDept")
    public AjaxResult alterDept(int deptId, String deptName) {
        Dept dept = new Dept();
        dept.setDeptId(deptId);
        dept.setDeptName(deptName);
        if (deptService.updateDeptById(dept)) {
            return AjaxResult.success("修改成功");
        } else {
            return AjaxResult.error("修改失败");
        }
    }

    /**
     * 创建结构树节点
     *
     * @param upId
     * @param deptName
     * @return
     */
    @RequiresRoles("admin")
    @ResponseBody
    @PostMapping("/insertDept")
    public AjaxResult insertDept(int upId, String deptName) {
        Dept dept = new Dept();
        dept.setDeptName(deptName);
        dept.setParentId(upId);
        dept.setAncestors(deptService.getByDeptId(upId).getAncestors().concat("," + upId));
        if (deptService.saveDept(dept)) {
            return AjaxResult.success("添加成功");
        } else {
            return AjaxResult.error("添加失败");
        }
    }
}
